import router from '@/router'
import { MessageBox } from 'element-ui'
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { isHttp, isEmpty } from '@/utils/validate'
import defAva from '@/assets/images/profile.jpg'

const user = {
  state: {
    token: getToken(),
    id: '',
    name: '',
    nickName: localStorage.getItem('nickName') || '',
    avatar: localStorage.getItem('avatar') || defAva,
    userInfo: JSON.parse(localStorage.getItem('userInfo') || '{}'),
    roles: [],
    permissions: [],
  },
  getters: {
    userInfo: state => state.userInfo, // 定义 getter，供组件获取
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USER_INFO(state, userInfo) {
      const { name, icon } = userInfo
      state.nickName = name || ''
      state.avatar = icon || ''
      state.userInfo = { ...userInfo }
      localStorage.setItem('nickName', state.nickName)
      localStorage.setItem('avatar', state.avatar)
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
    },
    CLEAR_USER_INFO(state) {
      state.token = ''
      state.nickName = ''
      state.avatar = ''
      state.userInfo = {}
      removeToken()
      localStorage.removeItem('nickName')
      localStorage.removeItem('avatar')
      localStorage.removeItem('userInfo')
    },
  },

  actions: {},
}

export default user
